#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Copyright © 启明星辰 版权所有
# @Time    : 2023/4/13 12:02
# @Author  : 胡浩浩
# @Email   : hu_haohao@venusgroup.com.cn
# @File    : json_tools.py
# @IDE     : PyCharm
# @description :
import json


def read_json_file(file_path):
    try:
        with open(file_path, 'r', encoding="utf-8") as f:
            data = json.load(f)
    except:
        data = {}
    return data


def write_json_file(file_path, data):
    with open(file_path, 'w', encoding="utf-8") as f:
        json.dump(data, f, ensure_ascii=False)


def write_to_file(file_path, data, model=None):
    if model == "b":
        with open(file_path, 'wb') as f:
            f.write(data)
    elif model == "a":
        with open(file_path, 'a', encoding="utf-8") as f:
            f.write(data)
    else:
        with open(file_path, 'w', encoding="utf-8") as f:
            f.write(data)


def clean_file(file_path):
    with open(file_path, "w") as f:
        f.truncate(0)


def read_file(file_path, model=None):
    if model:
        with open(file_path, 'rb') as f:
            data = f.read()
    else:
        with open(file_path, 'r', encoding="utf-8") as f:
            data = f.read()
    return data


def append_json_file(file_path, data):
    with open(file_path, 'a') as f:
        json.dump(data, f, ensure_ascii=False)
        f.write('\n')


if __name__ == '__main__':
    data = {'name': 'Jane', 'age': 25, 'city': 'Los Angeles'}
    append_json_file('../data/func_data.json', data)
